#!/usr/bin/bash
# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

# This script runs bitmex_instrument_feed_printer to generate csv files.
# After feed related code change, run this test to ensure
# that the code change does not broke basic stuff. There are two cases in
# which this test will fail: 1) There are bugs in the last commit,
# or 2) book building process is changed so that it generate different time
# series.


GIT_DIR=$(git rev-parse --git-dir)
REPO_TOP=$(dirname ${GIT_DIR})
cd "${REPO_TOP}/python"

RESULTS=(
"679cc107fe2a05bf8c441ed0eed1a937 BTC-USD.PERPETUAL.Bitmex.XBTUSD_20180819.csv"
"0dba95f1c05b62ed4df106b67f380f98 ETH-USD.PERPETUAL.Bitmex.ETHUSD_20180819.csv"
)

TEMP_CSV_DIR=$(mktemp -d)

export TZ="UTC"
./pyrunner coin/support/feed_tool/feed_research/bitmex_instrument_feed_printer.py \
  --trading_date=20180819 \
  --machine=feed-01.eu-west-1.aws \
  --csv_root=${TEMP_CSV_DIR} \
  --hours=1


check_md5() {
  local md5sum=${1}
  local filename="${TEMP_CSV_DIR}/${2}"
  if [[ ! -f ${filename} ]]; then
    echo "Regression test failed: csv file not generated." ${2}
    rm -rf ${temp_dir}
    exit 1
  fi

  local computed_md5sum=$(md5sum ${filename} | awk '{print $1}')
  if [[ "${md5sum}" != "${computed_md5sum}" ]]; then
    echo "Regression test failed: MD5 checksum not match." ${2}
    rm -rf ${temp_dir}
    exit 1
  fi
}

for ((n=0; n < ${#RESULTS[@]}; n++)); do
  check_md5 ${RESULTS[$n]}
done

rm -rf ${TEMP_CSV_DIR}
echo "Regression test passed."
